<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 22.04.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class adminControlPanel
    ****
    * This class represents the Control Panel Page of any solution
    ****
    * @package adminModules
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 22.04.2005
    * @copyright essentialmind.com 2005
    ***/
    class adminControlPanel extends adminModule{

        /**
         * adminControlPanel::process()
         * 
         * Process user inputs
         * 
         * @return void 
         **/
        public function process(){
            //is there a post to process?
            if($_POST){
                $this->processPost();    
            }
            
            //get recent folder changes
            $this->smarty->assign('pages',$this->db->getAll("SELECT 
                                                                    date_trunc('minute',pm.created) AS created, 
                                                                    date_trunc('minute',pm.modified) AS modified, 
                                                                    pm.id, 
                                                                    pm.owner_uid AS uid,
                                                                    ua.login AS user,
                                                                    pm.owner_gid AS gid,
                                                                    pm.title,
                                                                    om.title AS folder,
                                                                    om.nodeid AS folderid,
                                                                    dt.name AS datatype,
                                                                    get_path(pm.nodeid)||pm.pageurl||'.html' AS url
                                                                FROM pagemap AS pm
                                                                LEFT JOIN user_access AS ua ON (pm.owner_uid = ua.id)
                                                                LEFT JOIN objectmap AS om ON (pm.nodeid = om.nodeid)
                                                                LEFT JOIN datatype AS dt ON (pm.pagetype = dt.id)
                                                                ORDER BY modified DESC 
                                                                LIMIT 12"));
            //get admin accounts
            $this->smarty->assign('admins',$this->db->getAll("SELECT
                                                                    ud.fullname,
                                                                    ua.login,
                                                                    ua.id
                                                                FROM user_access AS ua
                                                                LEFT JOIN user_data AS ud ON ua.id = ud.uid
                                                                LEFT JOIN user_in_group AS ug ON ua.id = ug.uid
                                                                WHERE ug.gid = 1 ORDER BY ua.login"));
        }
        
        
        /**
         * In case there was a post request, process it
         *
         * Currently there is only one type of POST - change password
         * however, more may be applied later
         */
        private function processPost(){
            //user changing his/her password
            if(isset($_POST['pass1'])){
                //initialize errors array
                $errors = array();
                
                //check if password is long enough
                if(strlen($_POST['pass1'])<4){
                    $errors['pass1'] = true;
                }
                
                //check if the password and the confirm password strings are identical
                if($_POST['pass1'] != $_POST['pass2']){
                    $errors['pass2'] = true;
                }
                
                //any errors?
                if(empty($errors)){
                    //change password
                    $this->db->Execute("UPDATE user_access SET passwd = md5('".addslashes($_POST['pass1'])."') WHERE id = ".$_SESSION['admin']['id']);
                    
                    //log action
                    $this->log->recordAction("Successfully changes password for user -- \"".$_SESSION['admin']['login']."\"");
                    
                    //reset POST
                    unset($_POST);
                    
                    //redirect
                    header('Location: '.$GLOBALS['_CONFIG']['admin_root'].$_GET['view']  .'/?confirmPassChange=true');
                    exit;
                    
                }else{
                    $this->smarty->assign('changePassErrors',$errors);   
                }
            }    
        }
        

        /**
         * adminControlPanel::setTemplate()
         * 
         * Set action template to controlPanel.html
         * 
         * @return void
         **/
        protected function setTemplate(){
            $this->setActionTemplate('controlPanel.html');
        }
    }
?>